<?php
namespace app\controllers;
use app\models\Class1;
use app\models\Comment;
use app\models\SpeakPerson;
use app\models\User;
use app\models\WritePerson;
use yii\db\Query;
use \yii\web\Controller;
use yii\web\Response;

class CommunitydiscussController extends Controller
{
    public $layout = 'communitydiscuss';
    public function actionSearch()
    {
        $request=\Yii::$app->request;
        if($request->isPost)
        {
            $part=$request->post('part');
            $content=$request->post('content');
            $reading_id=Class1::find()->select(['id'])->where(['name'=>'reading'])->one()['id'];
            $writing_id=Class1::find()->select(['id'])->where(['name'=>'writing'])->one()['id'];
            $speaking_id=Class1::find()->select(['id'])->where(['name'=>'speaking'])->one()['id'];
            if (!isset($part)||!isset($content)) die();
            switch ($part)
            {
                case 'content':
                    $where=['like','comment_content',$content];
                    $andWhere=['and',
                        'user.user_id=comment.user_id',
                        "comment.class2_id=class2.id",
                        "comment.class1_id=class1.id",
                        "comment.class1_id<>$reading_id"];
                    break;
                case 'writing':
                    $where=['like','comment_content',$content];
                    $andWhere=['and',
                        'user.user_id=comment.user_id',
                        "comment.class2_id=class2.id",
                        "comment.class1_id=class1.id",
                        "comment.class1_id=$writing_id"];
                    break;
                case 'speaking':
                    $where=['like','comment_content',$content];
                    $andWhere=['and',
                        'user.user_id=comment.user_id',
                        "comment.class2_id=class2.id",
                        "comment.class1_id=class1.id",
                        "comment.class1_id=$speaking_id"];
                    break;
                case 'writer':
                    $where=['like','user.name',$content];
                    $andWhere=['and',
                        'user.user_id=comment.user_id',
                        "comment.class2_id=class2.id",
                        "comment.class1_id=class1.id",
                        "comment.class1_id<>$reading_id"];
                    break;
                default:
                    die("{state:'error'");
                    break;
            }
            $query = new Query();
            $from=['comment', 'user', 'class2', 'class1'];
            $select = [
                'user.name as user_name',
                'play_num',
                'like_num',
                'question_id',
                'url',
                'comment_content',
                'comment_time',
                'class2.name as class2_name',
                'class1.name as class1_name',
                'class1.id as class1_id',
                'class2.id as class2_id',
                'user.user_id as tar_id',
                'like_ids',
                'tnum'
            ];
            $comment = $query
                ->select($select)
                ->from($from)
                ->where($where)
                ->andWhere($andWhere)
                ->orderBy(['like_num'=>SORT_DESC])
                ->all();
            return $this->render('search',['comment'=>$comment]);
        }
        return $this->redirect('?r=communitydiscuss/sq');
    }

    public function actionSq()
    {
        return $this->render('sq');
    }
}